<?php
/**
 * 统计用户发表文章数量
 * User: yuyang
 * Date: 14-1-10
 * Time: 上午10:19
 */

class PoststatisticsController extends Controller{
    public function index(){
        $this->getSmarty();
        $this->smarty->display("admin/poststatistics/poststatistics.tpl");
    }

    public function userPostCountList(){
        $dbutil = $this->getDB();

        $page = $_POST["page"];
        //每页显示记录数
        $rows = $_POST["rows"];
        $start = ($page-1)*$rows ;
        $end = $page*$rows;
        //排序索引id
        $sidx = $_POST["sidx"];
        //排序顺序
        $sord = $_POST["sord"];

        $date_from = $_POST["date_from"];
        $date_to = $_POST["date_to"];
        $user_name =$_POST["user_name"];
        $usersql="";
        $usercount="select count(*) usercount from admin_users";
        $quarylistsql = "SELECT id,user_name,(SELECT	count(*) FROM	posts po WHERE ";

        if($user_name !=""){
            $quarylistsql.="po.post_author=(select id from admin_users where user_name='".$user_name."')";
            $usersql.="where adminu.user_name='".$user_name."' ";
        }else{
            $quarylistsql=$quarylistsql."po.post_author = adminu.id ";
        }

        if($date_from !="" && $date_to !=""){
            $quarylistsql.=" AND po.post_date BETWEEN STR_TO_DATE('".$date_from."','%Y-%m-%d') and STR_TO_DATE('".$date_to."','%Y-%m-%d')) postcount";
            $quarylistsql.=" ,concat('".$date_from."','--','".$date_to."') userdate";
        }else if($date_from != ""){
            $quarylistsql.=" AND po.post_date >= STR_TO_DATE('".$date_from."','%Y-%m-%d')) postcount";
            $quarylistsql.=",concat('".$date_from."','--',date_format(SYSDATE(), '%Y-%m-%d')) userdate";
        }else if($date_to != ""){
            $quarylistsql.=" AND po.post_date <= STR_TO_DATE('".$date_to."','%Y-%m-%d')) postcount";
            $quarylistsql.= ",concat((select min(post_date) from posts po where post_author = adminu.id),'--','".$date_to."') userdate";
        }else{
            $quarylistsql.=" AND date_format(po.post_date, '%Y-%m') = date_format(SYSDATE(), '%Y-%m')) postcount ";
            $quarylistsql.=",concat(date_format(SYSDATE(), '%Y-%m-01'),'--',date_format(SYSDATE(), '%Y-%m-%d')) userdate ";
        }

        $quarylistsql.=" FROM admin_users adminu ";
        if($usersql !=""){
            $quarylistsql.=$usersql;
        }
        $quarylistsql.="ORDER BY "."$sidx $sord"." limit $start ,$end";

        $result = $dbutil->get_results($quarylistsql);
        $data = array();
        foreach ($result as $res){
            $data[] = array(
                "cell"=>array(
                    $res->user_name,
                    $res->postcount,
                    $res->userdate,
                    $res->id,
                )
            );
        }
        $usercount = $dbutil->get_row($usercount);
        $count = $usercount->usercount;
        $totalPage = ceil($count/$rows);
        $result = array(
            "rows"=>$data,
            "page"=>$page,
            "total"=>$totalPage,
            "records"=>$count
        );
        echo json_encode($result);
    }


    function getuserpost($params){

        //得到用户ID
        $user_id =$params['user_id'];
        $page = $_POST["page"];
        //每页显示记录数
        $rows = $_POST["rows"];
        $start = ($page-1)*$rows ;
        $end = $page*$rows;
        //排序索引id
        $sidx = $_POST["sidx"];
        //排序顺序
        $sord = $_POST["sord"];
        $userpostsql = "select post_date,post_title,ID,post_category from posts where  post_author = '".$user_id."'";

        $date_from = $params["date_from"];
        $date_to = $params["date_to"];
        $commonsql ="";
        if($date_from !="" && $date_to !=""){
            $commonsql=" AND post_date BETWEEN STR_TO_DATE('".$date_from."','%Y-%m-%d') and STR_TO_DATE('".$date_to."','%Y-%m-%d')";
        }else if($date_from != ""){
            $commonsql=" AND post_date >= STR_TO_DATE('".$date_from."','%Y-%m-%d')";
        }else if($date_to != ""){
            $commonsql=" AND post_date <= STR_TO_DATE('".$date_to."','%Y-%m-%d')";
        }else{
            $commonsql=" AND date_format(post_date, '%Y-%m') = date_format(SYSDATE(), '%Y-%m')";
        }
        $dbutil = $this->getDB();
        $userpostsql.=$commonsql." ORDER BY "."$sidx $sord"." limit $start ,$end";
        $results = $dbutil->get_results($userpostsql);
        require_once SERVICE . DS . 'IndexService.class.php';
        $indexService = new IndexService($dbutil);
        $indexService->fillPostHref($results);
        $data = array();
        foreach ($results as $res){
            $data[] = array(
                "cell"=>array(
                    trim($res->post_title),
                    $res->post_date,
                    "<a href='".$res->href."'  target='_blank'>详细</a>"
                )
            );
        }
        $conutsql = "select count(*) usercount from posts where post_author = '".$user_id."' ".$commonsql;
        $usercount = $dbutil->get_row($conutsql);
        $count = $usercount->usercount;
        $totalPage = ceil($count/$rows);
        $result = array(
            "rows"=>$data,
            "page"=>$page,
            "total"=>$totalPage,
            "records"=>$count
        );
        echo  json_encode($result);
    }
}